/*************************************************************************
 *
 * definice a inicializace globalnich dat
 * 
 * (c) Ales Povalac, alpov@alpov.net, http://www.alpov.net/
 * 
 *************************************************************************
 *
 * $Id: data.c 83 2006-08-11 09:25:54Z alpov $
 * 
 *************************************************************************/         

#include <stdint.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include "../avrx/avrx.h"
#include "../avrx/AvrXFifo.h"
#include "data.h"
#include "menu_lcd.h"

Mutex DataLock;

DESC_STRUCT EEPROM desc = {
    .sensor_desc = {"Esko", "Becko", "Externi"},
    .sensor_count = 3,
    .mode_desc = {"Vypnuto", "Ekvitermni", "Termostat", "Rezim A - silny", "Rezim B - slaby", "Rezim C - staly"},
    .mode_count = 6,
};

EEDATA_STRUCT EEPROM eedata_eeprom = {
    .addr = 3,
    .contrast = 50
};

EEDATA_STRUCT eedata;

STDATA_STRUCT stdata;

RWDATA_STRUCT rwdata;
RWDATA_STRUCT rwdata_undo;


/*************************************************************************
 * nacteni internich promennych z eeprom
 *************************************************************************/ 
void ReadEepromData(void)
{
    ReadEEPromBlock(&eedata, &eedata_eeprom, sizeof(EEDATA_STRUCT));
    LcdSetContrast(eedata.contrast);
}


/*************************************************************************
 * zapis internich promennych do eeprom
 *************************************************************************/ 
void WriteEepromData(void)
{
    WriteEEPromBlock(&eedata, &eedata_eeprom, sizeof(EEDATA_STRUCT));
}


/*************************************************************************
 * nacteni bloku z eeprom (napr. textu)
 *************************************************************************/ 
void ReadEEPromBlock(void *pointer_ram, void *pointer_eeprom, unsigned char n)
{
    AvrXWaitSemaphore(&EEPromMutex);
    eeprom_busy_wait();
    eeprom_read_block(pointer_ram, pointer_eeprom, n);
    AvrXSetSemaphore(&EEPromMutex);
}


/*************************************************************************
 * zapis bloku do eeprom (napr. textu)
 *************************************************************************/ 
void WriteEEPromBlock(void *pointer_ram, void *pointer_eeprom, unsigned char n)
{
    AvrXWaitSemaphore(&EEPromMutex);
    eeprom_busy_wait();
    eeprom_write_block(pointer_ram, pointer_eeprom, n);
    AvrXSetSemaphore(&EEPromMutex);
}

